package com.example.mybaiduweishi.fragments;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.mybaiduweishi.MyApplication;
import com.example.mybaiduweishi.R;
import com.example.mybaiduweishi.activity.InsertContactsActivity;
import com.example.mybaiduweishi.fragments.base.BaseFragmentbase;
import com.example.mybaiduweishi.listener.OnRecyclerItemClickListener;
import com.example.mybaiduweishi.utils.MyUtil;
import com.example.mybaiduweishi.utils.ToastUtil;
import com.example.mybaiduweishi.views.CircleTextImageView;
import com.example.mybaiduweishi.vo.Contacts;
import com.github.lzyzsd.randomcolor.RandomColor;

import java.util.Collections;
import java.util.List;

import butterknife.BindView;

/**
 * Created by Administrator on 2017/6/5 0005.
 */

public class ShowContactsFragment extends BaseFragmentbase {
    static List<Contacts> listSos = MyApplication.getSoslist();  //求救联系人
    @BindView(R.id.show_contacts_fragment)
    RecyclerView recyclerView;
    private static int count;
    private MyContactsAdapter adapter;
    private List<Contacts> list;//所有联系人
    public static TextView tv_count ;


    @Override
    public int getXML() {
        return R.layout.show_contacts_fragment;
    }

    @Override
    public void initView() {
        //refreshbyeditOnclick
        tv_count = (TextView) getActivity().findViewById(R.id.text_contact_count);
    }

        @Override
        public void initData() {
            tv_count.setText("("+MyApplication.getSoslist().size()+")");
            //1、得到数据
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                list = MyUtil.getAllContacts();
//            }
//        }).start();
            list  = MyApplication.getList();
            //2、适配器
            adapter = new MyContactsAdapter(list);
            RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
            recyclerView.setLayoutManager(layoutManager);
            recyclerView.setAdapter(adapter);
        }



         static class MyContactsAdapter extends RecyclerView.Adapter<MyContactsAdapter.MyViewHolder> {
             private List<Contacts> list;

             public MyContactsAdapter(List<Contacts> list) {
                 this.list = list;
             }

             @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_show_contacts, parent, false);
            MyViewHolder holder = new MyViewHolder(view);
            return holder;
        }

        @Override
        public void onBindViewHolder(MyViewHolder holder, int position) {
            //绑定数据
            Contacts contacts = list.get(position);
            holder.ctiv.setText(contacts.getName().substring(1));
            //颜色随机
            RandomColor randomColor = new RandomColor();
            holder.ctiv.setFillColor(randomColor.randomColor());
            holder.tv_username.setText(contacts.getName());
            holder.tv_telphone.setText(contacts.getTelnumber());
            holder.tv_address.setText(contacts.getAddress());
        }
        @Override
        public int getItemCount() {
            return list.size();
        }

        //组件实例化
        class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
            CircleTextImageView ctiv;
            TextView tv_username;
            TextView tv_address;
            TextView tv_telphone;
            CheckBox ck_choose;

            public MyViewHolder(View itemView) {
                super(itemView);
                ctiv = (CircleTextImageView) itemView.findViewById(R.id.ctiv);
                tv_username = (TextView) itemView.findViewById(R.id.tv_name);
                tv_address = (TextView) itemView.findViewById(R.id.tv_address);
                tv_telphone = (TextView) itemView.findViewById(R.id.tv_telphone);
                ck_choose = (CheckBox) itemView.findViewById(R.id.check_choose);
                itemView.setOnClickListener(this);
            }

            @Override
            public void onClick(View v) {
                /**
                 * 1、联系人列表点击事件
                 */
                int i  = getAdapterPosition();
                Contacts c = list.get(i);
                String tel = c.getTelnumber(); //号码
                tel.replace(" ","").replace("-","");
//                Toast.makeText(MyApplication.getContext(), "点击了 "+c.getName(), Toast.LENGTH_SHORT).show();
                //2、点击ITem 多选框选中
                //再次选中改变多选框
                /**
                 * 3、限制最大选择联系人数量 ： 3
                 */
                //选择联系人数量
                tv_count.setTextSize(18);
                if (tel != null && tel.matches("^1[34578]\\d{9}$") && !"".equals(tel)) {
                    if (ck_choose.isChecked()) {
                        ck_choose.setChecked(false);
//                        count--;
                        listSos.remove(c);
                        tv_count.setText("("+listSos.size()+")");
                    } else {
                        if (listSos.size() < 3) {
                            ck_choose.setChecked(true);
                            if (!listSos.contains(c)) {
//                                count++;
                                listSos.add(c);
                                tv_count.setText("("+listSos.size()+")");
                            }
                        } else {
                            ToastUtil.showShortToast(MyApplication.getContext(),
                                    "你再点就有BUG了！！！");
                        }
                    }
                }else {
                    ToastUtil.showShortToast(MyApplication.getContext(), "只能选手机号码，不然就出BUG");
                }
                /**
                 * 4、回退异常    在添加Activity界面写 onBackPress方法
                 */
            }
        }
    }


}